/**
 * 
 */
package com.mdgafur.android_object_oriented_programming_AppDatabase;

import com.mdgafur.android_object_oriented_programming_AppDal.News;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * @author admin
 * 
 */
public class AppDataBaseManager {
	private SQLiteDatabase database;
	private DataBaseHelper databaseHelper;

	public AppDataBaseManager(Context context) {
		databaseHelper = new DataBaseHelper(context);
	}

	public boolean saveNew(String title, String link, String description,
			String pubDate, String imgUrl, String category) throws Exception {
		database = databaseHelper.getWritableDatabase();
		ContentValues values = newContentVAlues(title, link, description,
				pubDate, imgUrl, category);
		try {
			database.insert(DataBaseHelper.DATABASE_TABLE_NAME, null, values);
		} finally {
			database.close();
		}
		return true;

	}

	public News getNews()
	{
		database=databaseHelper.getReadableDatabase();
		String sqlString = "SELECT * FROM " + DataBaseHelper.DATABASE_TABLE_NAME;
		Cursor cursor = database.rawQuery(sqlString, null);
		if (cursor != null) {
			try {
				if (cursor.moveToFirst()) {
					int id = cursor.getInt(cursor.getColumnIndex("Id"));
					News news= new News(id);
					return news;
				}
			} finally {
				cursor.close();
				database.close();
			}
		}
		return null;
	}
	
	private ContentValues newContentVAlues(String title, String link,
			String description, String pubDate, String imgUrl, String category) {
		ContentValues values = new ContentValues();
		values.put(DataBaseHelper.COLUMN_TITLE, title);
		values.put(DataBaseHelper.COLUMN_LINK, link);
		values.put(DataBaseHelper.COLUMN_DESCROPTION, description);
		values.put(DataBaseHelper.COLUMN_DATE, pubDate);
		values.put(DataBaseHelper.COLUMN_IMAGE, imgUrl);
		values.put(DataBaseHelper.COLUMN_CATEGORY, category);
		return values;
	}
}
